// @/api/access.ts

import { defineFakeRoute } from 'vite-plugin-fake-server/client'
import dayjs from 'dayjs'
import { ref } from 'vue'

// 默认 token 2小时过期
// 这里设置的数值仅用于测试 refreshToken 是否生效，大小无意义
const expired = () => dayjs().add(2, 'hour').format('YYYY-MM-DD HH:mm:ss')

const userInfo = ref({
  uid: 2,
  username: 'common',
  nickname: '普通用户MOCK',
  phone: '13800000000',
  email: 'common@facker.com',
  remark: '数据来源mock',
})

export default defineFakeRoute([
  {
    url: '/access/login/send-sms',
    method: 'post',
    response: () => {
      return {
        success: true,
        message: '操作成功',
      }
    },
  },
  {
    url: '/access/login/:method',
    method: 'post',
    response: () => {
      return {
        success: true,
        message: '操作成功',
        data: {
          accessToken: 'eyJhbGciOiJIUzUxMiJ9.fake',
          refreshToken: 'eyJhbGciOiJIUzUxMiJ9.fakeRefresh',
          expires: expired(),
        },
      }
    },
  },
  {
    url: '/access/info',
    method: 'get',
    response: () => {
      return {
        success: true,
        message: '操作成功',
        data: userInfo.value,
      }
    },
  },
  {
    url: '/access/refresh-token',
    method: 'post',
    // 用于测试当刷新失效时的情况
    // statusCode: 401,
    response: () => {
      return {
        success: true,
        message: '操作成功',
        data: {
          accessToken: 'eyJhbGciOiJIUzUxMiJ9.newfake',
          refreshToken: 'eyJhbGciOiJIUzUxMiJ9.newfakeRefresh',
          expires: expired(),
        },
      }
    },
  },
])
